home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++,comp.lang.fortran
- Path: sldb6.slac.stanford.edu!fairfield
- From: fairfield@sldb6.slac.stanford.edu
- Subject: Re: Calling C++ from FORTRAN on VMS
- Message-ID: <1996Feb15.195246.1@sldb6.slac.stanford.edu>
- Sender: news@unixhub.SLAC.Stanford.EDU
- Organization: Stanford Linear Accelerator Center
- References: <31239D17.3159@stsci.edu>
- Date: Fri, 16 Feb 1996 03:52:46 GMT
-
- In article <31239D17.3159@stsci.edu>,
- Scott Stallcup <stallcup@stsci.edu> writes:
- > I need to call a C++ routine from a FORTRAN routine on both
- > VAX/VMS and AXP/VMS platforms.
-
- This probably should have been posted to comp.os.vms (or even
- comp.sys.dec) since there is little specific to either Fortran or
- C++ involved (except for name mangling, see below).
-
- > The C++ documentation fails to mention mixed langage programs
- > (other than calling C from C++)...
- >
- > Given the following example code, what compiler/linker options
- > will resolve the c++ reference ?
- >
- > ---------------------------------------------------------------
- > program tfor
- > c
- > call tcxx ()
- > c
- > end
- > ---------------------------------------------------------------
- >
- > #include <stdio.h>
- >
- > void tcxx (void)
- > {
- > printf ("Hello World\n");
- > }
- >
- > ---------------------------------------------------------------
- > $ fortran tfor
- > $ cxx tcxx
- > $ link tfor,tcxx
- > %LINK-W-NUDFSYMS, 1 undefined symbol:
- > %LINK-I-UDFSYM, TCXX
- > %LINK-W-USEUNDEF, undefined symbol TCXX referenced
- > in psect $LINK$ offset %X00000020
- > in module TFOR
-
- The problem is that your C++ procedure, tcxx, has undergone
- C++'s "name mangling". It's external name is not "TCXX", it is
- "TCXX__XV". How did I detemine that? I used a "dumb" way, and I'll
- give another.
-
- 1) DUMB WAY: Put the object file in a library and let the
- library routines read the object file and tell what's
- there:
-
- $ cxx tcxx
- $ lib/crea tc tcxx
- $ lib/list/ful/name tc
- .
- .
- Module TCXX Ident V1.0 Inserted 15-FEB-1996 19:21:46 1 symbol
- --> TCXX__XV
-
- (I've edited the library output for clarity)
-
-
- 2) A BIT SMARTER: Add the /MACHINE_CODE "switch" to the
- compile step, and look at the listing file produced:
-
- $ cxx tcxx/list/mach
- $ type tcxx.lis
-
- ----------------------------------------------------------------------------
- 1 #include <stdio.h>
- 675
- 676 void tcxx (void)
- 0010 TCXX__XV:
- --> 0000 0010 .entry TCXX__XV,^m<>
- 5E 04 C2 0012 subl2 #4,sp
- ----------------------------------------------------------------------------
-
- (Again, I've edited the output.) The important line is
- the ".entry TCXX__XV,^m<>" (and the one preceding it).
- The entry point name is what the linker sees.
-
- In order to _begin_ to do what you're attempting, you need to
- write the Fortran as,
-
- Program Tfor
- Call Tcxx__xv
- End
-
- That will satisfy the LINK. However, your printf may fail since the
- C/C++ run time initialization hasn't been done given that the main
- program is Fortran. Post to comp.os.vms for pointers on getting the
- initialization done, (it's simple, but I don't recall how to do it),
- and be advised that you want to use _either_ Fortran I/O, _or_ C/C++
- I/O, but not _both_ in the same program...or at the very least, not
- both to the same file (or terminal/keyboard).
-
- -Ken
- --
- Kenneth H. Fairfield | Internet: Fairfield@Slac.Stanford.Edu
- SLAC, P.O.Box 4349, MS 46 | DECnet: 45537::FAIRFIELD (45537=SLACVX)
- Stanford, CA 94309 | Voice: 415-926-2924 FAX: 415-926-3515
- -------------------------------------------------------------------------
- These opinions are mine, not SLAC's, Stanford's, nor the DOE's...
-